package xyz.hujf.myThreadPool;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Hujf
 * @title: HujfThreadPool
 * @date 2021-04-2311:59
 * @description: TODO
 */
public class HujfThreadFactory implements ThreadFactory {

    private static final AtomicInteger poolNum = new AtomicInteger(1);
    private final AtomicInteger threadNum = new AtomicInteger(1);
    private  ThreadGroup group;
    private String poolName;
    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(group,r,poolName+threadNum.getAndIncrement(),0);
        if(t.isDaemon()){
            t.setDaemon(false);
        }

        if(t.getPriority()!=Thread.NORM_PRIORITY){
            t.setPriority(Thread.NORM_PRIORITY);
        }
        return t;
    }


    public HujfThreadFactory(String name){
        SecurityManager s =  System.getSecurityManager();
        group =(s!=null)?s.getThreadGroup():Thread.currentThread().getThreadGroup();
        if(null ==name ||name.isEmpty()){
            name = "pool";
        }
        this.poolName = name+" "+threadNum;
    }

    public String getPoolName() {
        return poolName;
    }

    public void setPoolName(String poolName) {
        this.poolName = poolName;
    }


}
